package com.xnx3.banmahuishou.agency.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.xnx3.banmahuishou.core.entity.Daishoudian;
import com.xnx3.j2ee.service.SqlService;
import com.xnx3.j2ee.service.UserService;
import com.xnx3.j2ee.util.ActionLogUtil;
import com.xnx3.j2ee.util.Page;
import com.xnx3.j2ee.util.Sql;

/**
 * 代收点管理
 * @author 王晓龙
 */
@Controller(value="BanmahuishouAgencyDaishoudianController")
@RequestMapping("/agency/daishoudian/")
public class DaishoudianController extends BaseController {
	@Resource
	private SqlService sqlService;	
	@Resource
	private UserService userService;
	
	/**
	 * 代收点列表
	 */
	@RequestMapping("list${url.suffix}")
	public String list(HttpServletRequest request,Model model){
		Sql sql = new Sql(request);
		sql.setSearchColumn(new String[]{"daishoudian_name","state="});
		sql.appendWhere("dailishang_userid = " + getUserId());
		int count = sqlService.count("laji_daishoudian", sql.getWhere());
		Page page = new Page(count, 15, request);
		sql.setSelectFromAndPage("SELECT * FROM laji_daishoudian", page);
		sql.setDefaultOrderBy("laji_daishoudian.state ASC");
		List<Daishoudian> list = sqlService.findBySql(sql, Daishoudian.class);
		
		ActionLogUtil.insert(request, "代收点管理", "第"+page.getCurrentPageNumber()+"页");
		
		model.addAttribute("page", page);
		model.addAttribute("list", list);
		return "/agency/daishoudian/list";
	}

	/**
	 * 代收点详情
	 * @param id 要查看详情的代收点的id, 对应 laji_daishoudian.id
	 */
	@RequestMapping("view${url.suffix}")
	public String view(HttpServletRequest request,
			@RequestParam(value = "id", required = true) int id,Model model){
		Daishoudian daishoudian = sqlService.findById(Daishoudian.class, id);
		if(daishoudian == null){
			return error(model, "要查看的代收点不存在");
		}
		
		ActionLogUtil.insert(request, daishoudian.getId(), "代收点详情", daishoudian.toString());
		
		model.addAttribute("daishoudian", daishoudian);
		return "/agency/daishoudian/view";
	}
}
